package com.tbm.console.proj.json;

import com.tbm.console.common.constant.ConsoleConstant;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.base.controller.BaseController;
import com.tbm.console.proj.entity.ChartShowInfoEntity;
import com.tbm.console.proj.entity.LocalUserInfoEntity;
import com.tbm.console.proj.entity.SystemUser;
import com.tbm.console.proj.service.ChartShowInfoService;
import com.tbm.console.proj.service.LocalUserInfoService;
import com.tbm.console.util.DateUtil;
import com.tbm.console.util.JsonUtils;
import com.tbm.console.util.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//说明 字符串：ChartShowInfo，作为替换字符串 LOWERChartShowInfo

@RestController
@Slf4j
public class ChartShowInfoJson extends BaseController{

    @Autowired
    private ChartShowInfoService chartShowInfoService;

    @Autowired
    private LocalUserInfoService localUserInfoService;

    /**
     * 显示list
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/chartShowInfo-list/{page}/{size}")
    public Map<String,Object> chartShowInfoList(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageData pageData = this.getPageData();
        pageData.put("deleteFlag", "N");
        long count = chartShowInfoService.count(pageData);
        List<ChartShowInfoEntity> chartShowInfos = chartShowInfoService.findAll(pageData,page-1,size);
        pageData.put("total",count);
        pageData.put("rows", chartShowInfos);
        return pageData;
    }

    /**
     * 常量增删改
     * @return
     */
    @RequestMapping(value = "/chartShowInfo-ins-upd-del")
    public Object logCreateUpdate(){
        PageData pageData = this.getPageData();
        Integer id = -1;
        if(null != pageData.get("id")&&!"".equals(pageData.get("id").toString())){
            id = Integer.valueOf(pageData.get("id").toString());
        }
        ChartShowInfoEntity chartShowInfo = chartShowInfoService.findById(id);

        //------------------------------------------------------- 新增修改操作 ----------------------------------------------------------------------//

        if(-1==id){
            chartShowInfo.setCreateTime(DateUtil.getTime());
			chartShowInfo.setDeleteFlag(InsUpdDelFlagConstant.LOCALLOGDELETEFLAG);
        }else{
            chartShowInfo.setUpdateTime(DateUtil.getTime());
        }

        //字段信息维护
        if(null!=pageData.get("***")&&!"".equals(pageData.get("***").toString())){
			//目前只能手动添加，这里添加所需要增加和修改的字段
            //chartShowInfo.set***(pageData.get("***").toString());
        }
        
        //------------------------------------------------------- 删除操作 ---------------------------------------------------------------------------//
        if(null!=pageData.get("flag")&&"Y".equals(pageData.get("flag").toString())){
            chartShowInfo.setDeleteFlag(pageData.get("flag").toString());
        }
        chartShowInfoService.save(chartShowInfo);
        return null;
    }

    //保存用户自己的图表
    @RequestMapping(value = "/localUserInfo-chart-flag")
    public Object localUserInfoChartFlag(){

        PageData pageData = this.getPageData();
        String result = "";
        try{
            String ids = pageData.get("ids").toString();
            SystemUser systemUser = JsonUtils.jsonToObject(this.getRequest().getSession().getAttribute(ConsoleConstant.SESSIONUSER).toString(), SystemUser.class);
            PageData param = new PageData();
            param.put("userId", systemUser.getId());
            List<LocalUserInfoEntity> all = localUserInfoService.findAll(param);
            if(all.size()==1){
                LocalUserInfoEntity  temp = all.get(0);
                temp.setPassword(ids);
                localUserInfoService.save(temp);
                result = "success";
//                this.getRequest().getSession().removeAttribute(ConsoleConstant.SESSIONUSER);
//                this.getRequest().getSession().removeAttribute(ConsoleConstant.RIGHTS);
//                this.getRequest().getSession().removeAttribute(ConsoleConstant.ROLEID);
//                this.getRequest().getSession().removeAttribute(ConsoleConstant.BUTTONRIGHTS);
            }else {
                result = "noInfo";
            }

        }catch (Exception e){
            result = "error";
        }
        return result;

    }


}